import {Service} from 'egg';
import * as moment from 'moment';
import {ReqUpdateOrder, UpdateStatus} from '../../model/req_update_order';
import {OrderedScheduleStatus} from '../../model/ordered_schedule';

export default class DbReqUpdateOrderService extends Service {

    public async findOneById(id: number, mysql?: any): Promise<ReqUpdateOrder|null> {
        if (!mysql) {
            mysql = this.app.mysql;
        }
        const result = await mysql.get('req_update_order', { id });
        if (result) {
            return result as ReqUpdateOrder;
        }
        return null;
    }

    public async findNeedConfirmByTeacherId(teacherId: number) {
        const mysql = this.app.mysql;
        const today = moment().format('YYYY-MM-DD');
        return await mysql.query(`SELECT
                                    req_update_order.id as req_update_order_id,
                                    req_update_order.date as new_date,
                                    req_update_order.time_from as new_time_from,
                                    req_update_order.time_to as new_time_to,
                                    req_update_order.type as type,
                                    req_update_order.reason as reason,
                                    ordered_schedule.date as old_date,
                                    ordered_schedule.time_from as old_time_from,
                                    ordered_schedule.time_to as old_time_to,
                                    user.username as student_name
                                FROM req_update_order
                                INNER JOIN ordered_schedule ON 
                                    ordered_schedule.date >= ?
                                    AND teacher_id=?
                                    AND ordered_schedule.status=${OrderedScheduleStatus.UpdateNeedConfirm}
                                    AND ordered_schedule_id=ordered_schedule.id
                                INNER JOIN user ON user.id = ordered_schedule.student_id
                                WHERE req_update_order.status=${UpdateStatus.NeedConfirm}`,
            [ today, teacherId ]);
    }

    public async saveOne(reqUpdateOrder: ReqUpdateOrder, mysql?: any) {
        if (!mysql) {
            mysql = this.app.mysql;
        }
        await mysql.insert('req_update_order', {
            ordered_schedule_id: reqUpdateOrder.ordered_schedule_id,
            reason: reqUpdateOrder.reason,
            type: reqUpdateOrder.type,
            date: reqUpdateOrder.date,
            time_from: reqUpdateOrder.time_from,
            time_to: reqUpdateOrder.time_to,
            status: UpdateStatus.NeedConfirm,
        });
    }

    /**
     * 设置请假为结束状态
     */
    public async finishReq(id: number, mysql?: any) {
        if (!mysql) {
            mysql = this.app.mysql;
        }
        const options = {
            where: {
                id,
            },
        };
        await mysql.update('req_update_order', {
            status: UpdateStatus.Finished,
        }, options);
    }

    /**
     * 取请假请求
     */
    public async cancelReq(id: number, mysql?: any) {
        const options = {
            where: {
                id,
            },
        };
        if (!mysql) {
            mysql = this.app.mysql;
        }
        await mysql.update('req_update_order', {
            status: UpdateStatus.Canceled,
        }, options);
    }
}
